package com.zhida.smartcontrol.dialog.kgsjdialog;

import android.text.TextUtils;

import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.LogUtil;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KgsjDialogPresenter extends BasePresenterImpl<KgsjDialogContract.View> implements KgsjDialogContract.Presenter {
    private int mSize;
    private StringBuilder mSbWeek = new StringBuilder();
    private StringBuilder mSbWay = new StringBuilder();

    @Override
    public void showWayButton(String switchType) {
        mSize = getSize(switchType);
        mView.showButton(mSize);
    }

    @Override
    public void requestSetTime(List<LampBean> codeList, String[] timeArr, List<ButtonStyle> weekList, List<ButtonStyle> wayList) {
//        设置开关控制时间，适用于集控、二代开关
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        String mWay = getStr(wayList, true, mSbWay);
        if (TextUtils.isEmpty(mWay)) {
            mView.showMsg("请选择通道");
            return;
        }
        String mWeek = getStr(weekList, false, mSbWeek);
        if (TextUtils.isEmpty(mWeek)) {
            mView.showMsg("请选择日期");
            return;
        }
        int waySum = getSum(mWay, true);
        int weekSum = getSum(mWeek, false);
        LogUtil.e(TAG, mWay + "==" + waySum + "==" + mWeek + "==" + weekSum);

        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("checkedWaysString", mWay); //选中的通道组成的 字符串
        params.put("checkedWaysValue", waySum); //选中的通道的和
        params.put("checkedWeekdaysString", mWeek);
        params.put("weekdayValues", weekSum);
        params.put("firstStartTime", timeArr[0]);
        params.put("firstEndTime", timeArr[1]);
        params.put("secondStartTime", timeArr[2]);
        params.put("secondEndTime", timeArr[3]);
        params.put("thirdStartTime", timeArr[4]);
        params.put("thirdEndTime", timeArr[5]);
        mKalleCenter.postJson(Api.SET_CONTROL_TIMES, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    // true表示通道
    private String getStr(List<ButtonStyle> btnList, boolean isWay, StringBuilder sb) {
        sb.delete(0, sb.length());
        int size = isWay ? mSize : btnList.size();
        for (int i = 0; i < size; i++) {
            ButtonStyle btn = btnList.get(i);
            if (btn.isChecked()) {
                sb.append(isWay ? i + 1 : (int) Math.pow(2, i)).append(",");
            }
        }
        String str = sb.toString();
        if (!TextUtils.isEmpty(str)) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }

    private int getSum(String str, boolean isWay) {
        String[] strArr = str.split(",");
        int sum = 0;
        for (int i = 0; i < strArr.length; i++) {
            int anInt = Integer.parseInt(strArr[i]);
            sum += isWay ? Math.pow(2, anInt - 1) : anInt;
        }
        return sum;
    }

    private int getSize(String switchType) {
        int type = Integer.parseInt(switchType);
        int size;
        switch (type) {
            case Const.NB_SINGLE:
            case Const.ZIG_BEE_SINGLE:
                size = 1;
                break;
            case Const.NB_DOUBLE:
                size = 2;
                break;
            case Const.SWITCH:
                size = 8;
                break;
            default:
                size = 4;
                break;
        }
        return size;
    }

}
